package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.OrderMemberDao_zh;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组服务
 */
@Service(interfaceClass = OrderMemberService_zh.class)
@Transactional
public class OrderMemberServiceImpl_zh implements OrderMemberService_zh {
    @Autowired
    private OrderMemberDao_zh orderMemeberDao_zh;


    //新增检查组，同时需要关联检查项（设置多对多关系）
    public void add(Member member, Integer[] setmealIds) {
        //新增检查组
        orderMemeberDao_zh.add(member);
        Integer memberId = member.getId();//检查组id
        Date orderDate = member.getOrderDate();
        String orderType = member.getOrderType();
        String orderStatus = member.getOrderStatus();
        //设置检查组和检查项多对多关系
        this.setMemberAndSetmeal(memberId,setmealIds,orderDate,orderType,orderStatus);
    }

    public void setMemberAndSetmeal(Integer memberId,Integer[] setmealIds,Date orderDate, String orderType,String orderStatus){
        //设置多对多关系
        if(setmealIds != null && setmealIds.length > 0){
            for (Integer setmealId : setmealIds) {//检查项id
                Map<String,Object> map = new HashMap<>();
                map.put("memberId",memberId);
                map.put("setmealId",setmealId);
                map.put("orderDate",orderDate);
                map.put("orderType",orderType);
                map.put("orderStatus",orderStatus);
                orderMemeberDao_zh.setMemberAndSetmeal(map);
            }
        }
    }

    //分页查询
    @Override
    public PageResult findPage(Map map){
        Integer currentPage = (Integer) map.get("currentPage");
        Integer pageSize = (Integer) map.get("pageSize");
        List<String> date = (List<String>) map.get("date");
        if(date!=null&&date.size()>0){
         String[] s1 = date.get(0).split("T");
         String[] s2 = date.get(1).split("T");
         map.remove("date");
         String[] dates = {s1[0],s2[0]};
         map.put("date",dates);
        }
        PageHelper.startPage(currentPage,pageSize);
        Page<Map<String,Object>> orders = orderMemeberDao_zh.findByCondition(map);
        for (Map<String,Object> order : orders){
            Integer id = (Integer) order.get("id");
            order.remove("id");
            order.put("orderId",id);
            Integer setmeal_id = (Integer) order.get("setmeal_id");
            String setmealName = orderMemeberDao_zh.findSetmealById(setmeal_id);
            order.put("setmealName",setmealName);
        }
        long total = orders.getTotal();
        List<Map<String,Object>> result = orders.getResult();
        return new PageResult(total,result);
    }



    @Override
    public Member findById(Integer orderId) {
        return orderMemeberDao_zh.findById(orderId);
    }

    @Override
    public void delete(Integer id) {
            orderMemeberDao_zh.deleteById(id);
        }

    @Override
    public List<Integer> findSetmealIdsByOrderId(Integer orderId){
        return orderMemeberDao_zh.findSetmealIdsByOrderId(orderId);
    }


    @Override
    public void changStatus(Map map){
        orderMemeberDao_zh.changStatus(map);
    }




    //编辑检查项信息，同时需要设置关联关系
    public void edit(Member member) {
        //基本信息修改
        orderMemeberDao_zh.edit(member);
        //获取order的id
        Integer orderId = member.getOrderId();

        //更新member表
        Integer memberId = orderMemeberDao_zh.findByOrder(orderId);
        String name = member.getName();
        Map<String,Object> map = new HashMap<>();
        map.put("id",memberId);
        map.put("name",name);

        orderMemeberDao_zh.editMemberByMemberId(map);

    }

    public List<Member> findAll() {
        return orderMemeberDao_zh.findAll();
    }
}

